package com.zyit.web;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zyit.DataObject.T_user;
import com.zyit.service.AdminService;
import com.zyit.service.Impl.AdminServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/manager/user/toUpdateUser")
public class UpdataServlet extends HttpServlet {
    private AdminService adminService = new AdminServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer userId = Integer.valueOf(req.getParameter("userId"));
        if (userId != 0) {
            T_user tUser = adminService.IdByUser(userId);
            req.setAttribute("user", tUser);

            RequestDispatcher dispatcher = req.getRequestDispatcher("/manager/user/toUpdateUser.jsp");
            dispatcher.forward(req, resp);
        } else {
            req.getRequestDispatcher("/manager/user/toUpdateUser.jsp");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        T_user tUser = new T_user();
        req.setCharacterEncoding("UTF-8");

        tUser.setAccount(req.getParameter("account"));
        tUser.setNick_name(req.getParameter("nickName"));

        tUser.setId_card_no(req.getParameter("idCardNo"));
        tUser.setEmail(req.getParameter("email"));
        tUser.setPhone(req.getParameter("phone"));
        tUser.setType(Integer.valueOf(req.getParameter("type")));



        //修改
        if (req.getParameter("id") != "") {
            tUser.setId(Integer.valueOf(req.getParameter("id")));
            System.out.println(tUser);
            adminService.idUpdataUser(tUser);

            resp.sendRedirect("/manager/user/userList.jsp");
            //添加
        } else {
            System.out.println("添加");
            tUser.setPassword(req.getParameter("password"));
            adminService.addUser(tUser);
            resp.sendRedirect("/manager/user/userList.jsp");
        }

    }
}
